using WeatherSystem.API.Models;

namespace WeatherSystem.API.Specifications
{
    /// <summary>
    /// 城市查询规范
    ///
    /// 提供多种城市查询方式，支持按活跃状态、搜索关键词、国家等条件筛选
    /// 所有查询默认按城市名称升序排序
    /// </summary>
    public class CitySpecification : BaseSpecification<City>
    {
        /// <summary>
        /// 综合查询构造函数
        /// </summary>
        /// <param name="isActive">活跃状态过滤</param>
        /// <param name="search">搜索关键词（匹配城市名或国家）</param>
        /// <param name="country">国家过滤</param>
        public CitySpecification(bool? isActive = null, string? search = null, string? country = null)
            : base(c =>
                (!isActive.HasValue || c.IsActive == isActive.Value) &&
                (string.IsNullOrEmpty(search) || c.Name.Contains(search) || c.Country.Contains(search)) &&
                (string.IsNullOrEmpty(country) || c.Country == country))
        {
            ApplyOrderBy(c => c.Name);
        }

        /// <summary>
        /// 根据ID查询城市
        /// </summary>
        /// <param name="id">城市ID</param>
        public CitySpecification(Guid id)
            : base(c => c.Id == id)
        {
        }

        /// <summary>
        /// 根据名称查询城市
        /// </summary>
        /// <param name="name">城市名称</param>
        public CitySpecification(string name)
            : base(c => c.Name == name)
        {
        }

        /// <summary>
        /// 根据名称和国家查询城市
        /// </summary>
        /// <param name="name">城市名称</param>
        /// <param name="country">国家名称</param>
        public CitySpecification(string name, string country)
            : base(c => c.Name == name && c.Country == country)
        {
        }

        /// <summary>
        /// 根据名称查询城市，排除指定ID
        /// </summary>
        /// <param name="name">城市名称</param>
        /// <param name="excludeId">要排除的城市ID</param>
        public CitySpecification(string name, Guid excludeId)
            : base(c => c.Name == name && c.Id != excludeId)
        {
        }

        /// <summary>
        /// 精确匹配查询构造函数
        /// </summary>
        /// <param name="name">城市名称</param>
        /// <param name="country">国家名称</param>
        /// <param name="isActive">活跃状态</param>
        public CitySpecification(string? name, string? country, bool? isActive = null)
            : base(c =>
                (string.IsNullOrEmpty(name) || c.Name == name) &&
                (string.IsNullOrEmpty(country) || c.Country == country) &&
                (!isActive.HasValue || c.IsActive == isActive.Value))
        {
            ApplyOrderBy(c => c.Name);
        }

        /// <summary>
        /// 自定义谓词查询构造函数
        /// </summary>
        /// <param name="predicate">自定义查询条件</param>
        public CitySpecification(System.Linq.Expressions.Expression<Func<City, bool>> predicate)
            : base(predicate)
        {
        }
    }
}